var myUtil ={

    /**
   * 二维表数据转树形数据
   * @param {*} arr 二维表数据
   * @param {*} topParentId 根节点的父节点命名
   * @param {*} keyName 主键命名
   * @returns 返回生成的树形数据
   */
  list2TreeData:function(arr,topParentId,keyName){
    //1、深克隆
    let newArr = JSON.parse(JSON.stringify(arr))
    //2、定义一个新数组保存新的树型数据
    let treeData = [];
    for(var i in newArr){
      //3、从顶级根节点找子节点
      if(newArr[i].parentId == topParentId){
        let nowNode = this.getChildren(newArr,newArr[i],keyName)
        treeData.push(nowNode);
      }
    }
    return treeData
  },

  /**
   * 递归生成子节点
   * @param {*} arr 
   * @param {*} topParentId 
   * @param {*} keyName 
   * @returns 返回子节点
   */
  getChildren:function(arr,item,keyName){
    let newArr = JSON.parse(JSON.stringify(arr))
      let cArr = [];
      for(var c in newArr){
        if(newArr[c].parentId == item[keyName]){
          let node = this.getChildren(newArr,newArr[c],keyName)
          cArr.push(node)
        }
      }
      item.children = cArr
      return item;
  },
  //树形数据转二维数据
  treeData2List:function(treeData){
      // 1. 深克隆
      let newTreeData = JSON.parse(JSON.stringify(treeData));

      //  2. 结果数组
      let newArr = [];

      for(var i in newTreeData){

          newArr.push(newTreeData[i]);

          if(newTreeData[i].children.length>0){

              this.addChildrenToArr(newArr,newTreeData[i].children);

          }

      }

      for(var c in newArr){
          delete newArr[c].children;
      }
      
      return newArr;
  },
  addChildrenToArr:function(arr,children){
      for(var i in children){
          arr.push(children[i]);
          if(children[i].children.length>0){
              this.addChildrenToArr(arr,children[i].children);
          }
      }
  },
  getLocalId:function(){

    var localID = window.localStorage.getItem("localID");
    if(localID==null){
      var s = [];
      var hexDigits = "01234569875saflkj";
      for(var i = 0;i<36;i++){
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10),1);
      }
      s[14] = "4";
      s[19] = hexDigits.substr((s[19] & 0x3) | 0x8,1);
      s[8] = s[13] = s[18] = s[23] = "-";

      var uuid = s.join("");

      window.localStorage.setItem("localID",uuid);
      localID = uuid;
    }
    return localID;

  }
};

export default myUtil;
